import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)


const store = new Vuex.Store({
	state: {
		page: 0,
		audit: true, //是否审核
		id:"",//数据库设置id
		comment:true,
		msg:"",//消息评论
		checknum:"",//审核帖子
		num:0,//总数
	},
	mutations: {
		changePage(state, page) {
			state.page = page
		},
		changecheck(state, audit) {
			state.audit = audit
		},
		init(state,id){
			state.id = id
		},
		changeComment(state,comment){
			state.comment = comment
		},
		initmsgnum(state,msg){
			state.msg = msg
		},
		initchecknum(state,num){
			state.checknum = num
		},
		initnum(state,num){ //更新通知
			state.num = num
		}
	},
	actions: {
		async initcheck({commit}) { 
			let {result} = await uniCloud.callFunction({
				name: "admin",
				data:{
					type:"init"
				}
			})
			commit('changecheck',result.data[0].ok) 
			commit('init',result.data[0]._id)
		},
		async changecheck({commit,state },check) {
			let {result} = await uniCloud.callFunction({
				name: "admin",
				data:{
					type:"change",
					_id:state.id,
					check
				}
			})
		},
		async initmsgnum({commit},id){
			let res =  await uniCloud.callFunction({
				name: "get_message",
				data: {
					type: "get_msg",
					id
				}
			})
			let a = []
			res.result.map(items => {
				items.data.map(item => {
					a.push(item)
				})
			})
			commit('initmsgnum',a)
		},
		async initchecknum({commit}){
		let res =  await	uniCloud.callFunction({
				name: "get_content",
				data: {
					type: "no" //未审核帖子
				}
			})
			commit('initchecknum',res)
		}
	}
})
export default store
